﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Coding4Fun.Phone.Controls;
using System.Windows.Media.Imaging;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using System.ComponentModel;
using System.Windows.Threading;
using System.Windows.Data;

namespace WeiTalk
{
    public delegate DispatcherOperation Invoke(Action a);

    public delegate void GetDataStreamCallback(Stream stream);
    public delegate void GetPicCallback(BitmapSource bimage);
    public delegate void HandleImageSource(Stream source);

    public class WeiBoUtility
    {
       static  ToastPrompt toast = new ToastPrompt();
        public static void ShowToast(string message)
        {
            Grid parent = toast.Parent as Grid;
            if (parent != null)
            {
                parent.Children.Remove(toast);
            }
            toast = new ToastPrompt();
            toast.Message = message;//
            toast.FontSize = 18d;
            toast.FontWeight = FontWeights.Bold;

            toast.Background = new ImageBrush() {  ImageSource = new BitmapImage((new Uri("/Images/bg.png", UriKind.Relative))),  Opacity = 0.5};
           // toast.Background = new SolidColorBrush(Color.FromArgb(125,190, 190, 190));
            toast.Foreground = new SolidColorBrush(Color.FromArgb(255, 200, 200, 200));
            toast.Show(); 
            
        }

        public static void RetrieveImage(string picUrl, HandleImageSource callback, Invoke invoke)
        {
            HttpWebRequest http = WebRequest.Create(picUrl) as HttpWebRequest;
            var result = http.BeginGetResponse((ar) =>
            {
                HttpWebRequest theResponse = ((HttpWebRequest)ar.AsyncState);
                try
                {
                    WebResponse webResponse = theResponse.EndGetResponse(ar);
                    invoke(() =>
                    {
                        try
                        {
                            callback(webResponse.GetResponseStream());
                        }
                        catch
                        {

                        }
                        finally
                        {
                            webResponse.Close();
                            webResponse.Dispose();
                        }
                    });
                }
                catch (WebException) { }
            }, http);
        }

        static HttpWebRequest http = null;
        static HttpWebRequest webRequest = null;
        static WebResponse webResponse = null;

        public static void RetrieveImageByOne(string picUrl, HandleImageSource callback, Invoke invoke)
        {
            if (String.IsNullOrEmpty(picUrl))
            {
                throw new ArgumentNullException("URL 不正确");
            }
            if (http != null)
            {
                http.Abort();
            }
            if (webRequest != null)
            {
                webRequest.Abort();
            }
            if (webResponse != null)
            {
                webResponse.Close();
                webResponse.Dispose();
            }
            http = WebRequest.Create(picUrl) as HttpWebRequest;
            var result = http.BeginGetResponse((ar) =>
            {
                webRequest = ((HttpWebRequest)ar.AsyncState);
                try
                {
                    webResponse = webRequest.EndGetResponse(ar);
                    invoke(() =>
                    {
                        try
                        {
                            callback(webResponse.GetResponseStream());
                        }
                        catch
                        {

                        }
                    });
                }
                catch (WebException) { }
            }, http);
        }

    }

    public class ScrollViewerMonitor
    {
        private ScrollViewer scrollViewer;

        private static int index;

        public ScrollViewerMonitor(ScrollViewer scrollViewer)
        {
            this.scrollViewer = scrollViewer;

            var property = DependencyProperty.RegisterAttached("DependencyPropertyListener" + index++,
                typeof(double), typeof(ScrollViewer), new PropertyMetadata(scrollViewerVerticalOffset_Changed));

            Binding binding = new Binding("VerticalOffset") { Source = scrollViewer };
            scrollViewer.SetBinding(property, binding);
        }

        public event EventHandler AtEnd;

        private void scrollViewerVerticalOffset_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            bool atBottom = scrollViewer.VerticalOffset
                                    >= scrollViewer.ScrollableHeight;

            if (atBottom)
            {
                if (AtEnd != null)
                {
                    AtEnd(this, EventArgs.Empty);
                }
            }
        }
    }

}
